package cn.com.idmy.auth.event;


import cn.com.idmy.auth.login.LoginState;
import cn.com.idmy.base.util.Assert;

import java.util.ArrayList;
import java.util.List;

public class EventBus {
    private static List<Listener> listeners = new ArrayList<>();

    static {
        listeners.add(new ListenerLogImpl());
    }

    public static List<Listener> listListeners() {
        return listeners;
    }

    public static void setListeners(List<Listener> ins) {
        Assert.notNull(ins, "重置的侦听器集合不可以为空");
        listeners = ins;
    }

    public static void registerListener(Listener in) {
        Assert.notNull(in, "注册的侦听器不可以为空");
        listeners.add(in);
    }

    public static void registerListeners(List<Listener> ins) {
        Assert.notEmpty(ins, "注册的侦听器集合不可以为空");
        listeners.addAll(ins);
    }

    public static void removeListener(Listener listener) {
        listeners.remove(listener);
    }

    public static void removeListener(Class<? extends Listener> cls) {
        ArrayList<Listener> listenerListCopy = new ArrayList<>(listeners);
        for (Listener listener : listenerListCopy) {
            if (cls.isAssignableFrom(listener.getClass())) {
                listeners.remove(listener);
            }
        }
    }

    public static void clearListeners() {
        listeners.clear();
    }

    public static boolean hasListener(Listener in) {
        return listeners.contains(in);
    }

    public static boolean hasListener(Class<? extends Listener> cls) {
        for (Listener listener : listeners) {
            if (cls.isAssignableFrom(listener.getClass())) {
                return true;
            }
        }
        return false;
    }

    public static void doLogin(LoginState<?> login) {
        for (Listener listener : listeners) {
            listener.doLogin(login);
        }
    }

    public static void doLogout(Object id, String tokenValue) {
        for (Listener listener : listeners) {
            listener.doLogout(id, tokenValue);
        }
    }

    public static void doKickOut(Object id, String tokenValue) {
        for (Listener listener : listeners) {
            listener.doKickOut(id, tokenValue);
        }
    }

    public static void doReplaced(Object id, String tokenValue) {
        for (Listener listener : listeners) {
            listener.doReplaced(id, tokenValue);
        }
    }

    public static void doDisable(Object id, String service, int level, long disableTime) {
        for (Listener listener : listeners) {
            listener.doDisable(id, service, level, disableTime);
        }
    }

    public static void doUntieDisable(Object id, String service) {
        for (Listener listener : listeners) {
            listener.doUntieDisable(id, service);
        }
    }

    public static void doOpenTwo(String tokenValue, String service, long safeTime) {
        for (Listener listener : listeners) {
            listener.doOpenTwo(tokenValue, service, safeTime);
        }
    }

    public static void doCloseTwo(String tokenValue, String service) {
        for (Listener listener : listeners) {
            listener.doCloseTwo(tokenValue, service);
        }
    }

    public static void doCreateSession(String id) {
        for (Listener listener : listeners) {
            listener.doCreateSession(id);
        }
    }

    /**
     * 事件发布：一个 SaSession 注销了
     *
     * @param id SessionId
     */
    public static void doDeleteSession(String id) {
        for (Listener listener : listeners) {
            listener.doDeleteSession(id);
        }
    }

    /**
     * 事件发布：指定 Token 续期成功
     *
     * @param tokenValue token 值
     * @param id         账号id
     * @param exp        续期时间
     */
    public static void doRenewTokenTimeout(String tokenValue, Object id, long exp) {
        for (Listener listener : listeners) {
            listener.doRenewTimeout(tokenValue, id, exp);
        }
    }

    /**
     * 事件发布：有新的全局组件载入到框架中
     *
     * @param componentName 组件名称
     * @param component     组件对象
     */
    public static void doRegisterComponent(String componentName, Object component) {
        for (Listener listener : listeners) {
            listener.doRegisterComponent(componentName, component);
        }
    }

    /**
     * 事件发布：有新的 StpLogic 载入到框架中
     *
     * @param baseManager /
     */
    public static void doSetAuthManager(Object baseManager) {
        for (Listener listener : listeners) {
            listener.doSetAuthManager(baseManager);
        }
    }

    /**
     * 事件发布：有新的全局配置载入到框架中
     *
     * @param config /
     */
    public static void doSetGlobalProps(Object config) {
        for (Listener listener : listeners) {
            listener.doSetGlobalProps(config);
        }
    }
}
